package com.example.song.wlms.homefragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.song.wlms.R;
import com.example.song.wlms.adapter.OriginalAdapter;
import com.example.song.wlms.bean.OriginalData;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class OriginalFragment extends Fragment {
    private PullToRefreshListView listView;
    private String url_path = "http://api.viimoo.cn/resources/lists?authsign=&page=1&resources_type_id=0&resources_attr=1&sort=late";
    private List<OriginalData.DataBean> dataBeanList = new ArrayList<>();
    private OriginalAdapter adapter;
    private int index = 2;

    public OriginalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_original, container, false);
        initView(view);
        initgetData();
        listView.setMode(PullToRefreshListView.Mode.BOTH);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                url_path = "http://api.viimoo.cn/resources/lists?authsign=&page=1&resources_type_id=0&resources_attr=1&sort=late";
                dataBeanList.clear();
                index = 2;
                initgetData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                initgetData();
            }
        });
        return view;
    }

    private void initgetData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url_path).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                Gson gson = new Gson();
                url_path = "http://api.viimoo.cn/resources/lists?authsign=&page=" + index + "&resources_type_id=0&resources_attr=1&sort=late";
                index++;
                OriginalData originalData = gson.fromJson(response.body().string(), OriginalData.class);
                dataBeanList.addAll(originalData.getData());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (adapter == null) {
                            adapter = new OriginalAdapter(dataBeanList, getContext());
                            listView.setAdapter(adapter);
                        } else {
                            adapter.notifyDataSetChanged();
                        }
                        if (listView.isRefreshing()) {
                            listView.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listView.onRefreshComplete();
                                }
                            }, 1000);
                        }
                    }
                });
            }
        });
    }

    private void initView(View view) {
        listView = (PullToRefreshListView) view.findViewById(R.id.original_listView);
    }

}
